package com.coin900.coin.cmd.coin_project;

import com.coin900.coin.cmd.ACmd;
import com.coin900.coin.cmd.CmdSign;
import com.coin900.coin.code.CmdCode;
import com.coin900.coin.persist.coin.CoinProjectVO;
import com.coin900.coin.service.coin.ICoinProjectService;
import com.google.gson.Gson;
import com.value.mybatis.Pagination;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页加载项目
 *
 * @author shui
 * @create 2018-01-15
 **/
@Controller
public class CoinProjectListCmd extends ACmd {
    @Override
    public CmdSign execute(HttpServletRequest request, CmdSign sign) throws Exception {
        CmdSign respSign = new CmdSign(sign);
        Pagination pagination = respSign.buildPage();
        Map<String, Object> params = pagination.getParams();
        HashMap<String, Object> signParams = sign.getParams();
        String keyword = (String) signParams.get("keyword");
        if (keyword != null) {
            params.put("keyword", keyword);
        }
        params.put("dataStatus", 1);
        List<CoinProjectVO> coinProjectVOS = this.projectService.loadCoinProjectForPage(pagination);
        for (CoinProjectVO coinProjectVO : coinProjectVOS) {
            coinProjectVO.setReleaseUserName(coinProjectVO.getReleaseUserVO().getName());
            coinProjectVO.setTokenName(coinProjectVO.getTokenVO().getName());
        }
        Gson gson = new Gson();
        String source = gson.toJson(coinProjectVOS);
        respSign.setSource(source);
        respSign.setPageInfo(pagination);
        return respSign;
    }

    @Autowired
    private ICoinProjectService projectService;

    @Override
    public String getCmdCode() {
        return CmdCode.COIN_PROJECT_LIST;
    }
}
